package com.gxnu.edu.cn.ttxxt.controller;

import com.gxnu.edu.cn.ttxxt.entity.Users;
import com.gxnu.edu.cn.ttxxt.service.UserService;
import com.gxnu.edu.cn.ttxxt.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils; // 注入JwtUtils

//    @PostMapping("/register")
//    public ResponseEntity<?> register(@RequestBody Map<String, String> request) {
//        String username = request.get("username");
//        String password = request.get("password");
//        String phone = request.get("phone");
//        String email = request.get("email");
//
//        try {
//            User user = userService.registerUser(username, password, phone, email);
//            return ResponseEntity.ok("注册成功");
//        } catch (IllegalArgumentException e) {
//            return ResponseEntity.badRequest().body(e.getMessage());
//        }
//    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> credentials) {
        Map<String, Object> response = new HashMap<>();
        try {
            String username = credentials.get("username");
            String password = credentials.get("password");
            String ip = credentials.get("ip");

            if (username == null || username.isEmpty()) {
                response.put("code", 400);
                response.put("message", "用户名不能为空");
                return ResponseEntity.badRequest().body(response);
            }

            Map<String, Object> loginResponse = userService.login(username, password, ip);

            Map<String, Object> userInfo = new HashMap<>();
            Users user = (Users) loginResponse.get("user");
            userInfo.put("userId", user.getUserId());
            userInfo.put("username", user.getUsername());
            userInfo.put("avatarUrl", user.getAvatarUrl());
            userInfo.put("signature", user.getSignature());

            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", userInfo);
            response.put("token", loginResponse.get("token"));
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            response.put("code", 401);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "登录失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}
    